package cn.kgc.searcher.service;
/* q 关键字 分词功能 *:*  keyword
   fq 精确查找 or and mou're */
import cn.kgc.searcher.vo.HotelVo;
import cn.kgc.searcher.vo.SolrHotelBean;
import com.github.pagehelper.StringUtil;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;


public class TestSpringSolr {

    public static void main(String[] args) {
        SolrTemplate solrTemplate=new ClassPathXmlApplicationContext("spring-solr.xml").getBean(SolrTemplate.class);
        HotelVo hotelVo=new HotelVo();
        hotelVo.setKeywords("北京");
        Query query=new SimpleQuery("*:*");
        Criteria criteria=new Criteria("id");
        if (!StringUtil.isEmpty(hotelVo.getKeywords())){
            criteria=criteria.and("keyword").contains(hotelVo.getKeywords());
        }
        if (!(hotelVo.getHotelLevel()==null)){
            criteria=criteria.and("hotelLevel").is(hotelVo.getHotelLevel());
        }
        if (!StringUtil.isEmpty(hotelVo.getFeatureIds())){
            criteria=criteria.and("featureIds").contains(hotelVo.getFeatureIds().split(","));
        }
        if (!StringUtil.isEmpty(hotelVo.getDestination())){
            criteria=criteria.and("redundantCityName").contains(hotelVo.getDestination());
        }
        if (hotelVo.getMaxPrice()!=null&&hotelVo.getMaxPrice()!=0){
            criteria=criteria.and("maxPrice").between(0,hotelVo.getMaxPrice());
        }
        if (hotelVo.getMinPrice()!=null&&hotelVo.getMinPrice()!=0){
            criteria=criteria.and("minPrice").between(hotelVo.getMinPrice(),hotelVo.getMaxPrice());
        }
        query.addCriteria(criteria);
        ScoredPage<SolrHotelBean> page=solrTemplate.queryForPage("hotel",query,SolrHotelBean.class);
        System.out.println(page.getContent());
        /*PageUtil pageUtil=new PageUtil();
        pageUtil.setRows(page.getContent());
        pageUtil.setCurPage(1);
        pageUtil.setBeginPos(0);
        pageUtil.setTotal(page.getTotalElements());
        pageUtil.setPageSize(10);
        Dto<PageUtil> dto=new Dto<PageUtil>();
        dto.setMsg("搜索成功");
        dto.setData(pageUtil);*/
    }
}
